﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HTTT.DataAccess;
using HTTT.Utilities;
using log4net;
using HTTT.DTO;
using System.Runtime.Serialization;

namespace HTTT.Bussiness
{
    public enum GROUP_MEM_ACTION
    {
        NONE = 1
        , ADD
        , REMOVE
    }
     [Serializable]
    public class MyGroupMember
    {
        public Guid GroupId { get; set; }
        public HT_User User { get; set; }
        public GROUP_MEM_ACTION Action { get; set; }
        public MyGroupMember()
        {
        }
        public MyGroupMember(Guid groupId, HT_User user, GROUP_MEM_ACTION action)
        {
            GroupId = groupId;
            User = user;
            Action = action;
        }
    }
    public class HT_GroupsBO
    {
        public IEnumerable<HT_Groups> GetAllHT_Groups()
        {
            HT_GroupsDAO _dao = new HT_GroupsDAO();
            IEnumerable<HT_Groups> data = _dao.GetAllHT_Groups();
            return data;
        }
        public HT_Groups GetHT_GroupsById(Guid id)
        {
            HT_GroupsDAO _dao = new HT_GroupsDAO();
            return _dao.GetHT_GroupsByID(id);
        }
        public bool UpdateHT_Groups(HT_Groups inHT_Groups)
        {
            HT_GroupsDAO _dao = new HT_GroupsDAO();
            return _dao.UpdateHT_Groups(inHT_Groups);
        }
        public ReturnValue<bool> InsertHT_Groups(HT_Groups inHT_Groups)
        {
            HT_GroupsDAO _dao = new HT_GroupsDAO();
            return _dao.InsertHT_Groups(inHT_Groups);
        }

        public ReturnValue<HT_Groups> InsertHT_GroupsAndReturn(HT_Groups inHT_Groups)
        {
            HT_GroupsDAO _dao = new HT_GroupsDAO();
            return _dao.InsertHT_GroupsAndReturn(inHT_Groups);
        }

        public ReturnValue<bool> DelHT_Groups(Guid id)
        {
            HT_GroupsDAO _dao = new HT_GroupsDAO();
            return _dao.DelHT_Groups(id);
        }



        public ReturnValue<bool> SynNewData(List<GroupsDTO> listdata, string ouPath)
        {
            bool result = true;
            if (listdata == null || listdata.Count < 1)
            {
                return new ReturnValue<bool>(false, "Lấy dữ liệu không thành công!");
            }
            HT_GroupsDAO _dao = new HT_GroupsDAO();
            _dao.DelAllHT_GroupsInPath(ouPath);
            foreach (GroupsDTO group in listdata)
            {
                HT_Groups inGroup = new HT_Groups();
                inGroup.ID = Guid.NewGuid();
                inGroup.name = group.Name;
                inGroup.displayName = group.DisplayName;
                inGroup.description = group.Description;
                inGroup.groupId = group.groupGuid;
                inGroup.LDAPPath = group.LDAPPath;
                inGroup.groupScope = group.GroupScope;
                inGroup.isSecurityGroup = group.isSecurityGroup;
                inGroup.managedBy = group.ManagedBy;
                inGroup.members = MemberList2String(group.Members);
                result = result && _dao.InsertHT_Groups(inGroup).RetValue;
            }
            if (result)
            {
                return new ReturnValue<bool>(result, "Đồng bộ thành công " + listdata.Count + " bản ghi!");
            }
            else
            {
                return new ReturnValue<bool>(false, "Lấy dữ liệu không thành công!");
            }
        }

        private string MemberList2String(List<string> memberList)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string item in memberList)
            {
                sb.Append(item); sb.Append(Constants.MEMBERSPLIT);
            }
            return sb.ToString();
        }

        public IEnumerable<HT_Groups> SearchGroup(string keyword, string ouPath)
        {
            HT_GroupsDAO _dao = new HT_GroupsDAO();
            return _dao.SearchGroups(keyword, ouPath);
        }

        public bool RequestDeleteGroup(Guid ID, string requestedBy, string requestInfo)
        {
            //Get this user by ID
            HT_GroupsDAO dao = new HT_GroupsDAO();
            HT_Groups group = dao.GetHT_GroupsByID(ID);
            //Create reset pwd request
            HT_RequestDAO RequestDAO = new HT_RequestDAO();
            HT_Request request = new HT_Request();
            request.ID = Guid.NewGuid();
            request.Action = HT_ACTION.DELETE_GROUP.ToString();
            request.ActionName = "Xóa nhóm <strong>" + group.name + "</strong>";
            request.ActionDescription = group.name; //only username of this account
            request.Note = requestInfo;
            request.Requestedby = requestedBy;
            request.RequestDate = DateTime.Now;
            request.Status = HT_REQUESTSTATUS.PEDDING.ToString();
            return RequestDAO.InsertRequest(request);
        }

        public string BuildEditGroupString(string groupName, string description, string LDAPPath, int groupScope, int groupType, string members)
        {
            HT_Groups OldGroup = GetHT_GroupByName(groupName);
            StringBuilder sb = new StringBuilder();
            // Append new group infomation
            sb.Append(groupName); sb.Append(Constants.DATA_CUT);

            sb.Append(OldGroup.displayName); sb.Append(Constants.DATA_CUT); //displayName - Old info

            sb.Append(description); sb.Append(Constants.DATA_CUT);

            sb.Append(OldGroup.groupId); sb.Append(Constants.DATA_CUT);   //goupID - old info

            sb.Append(LDAPPath); sb.Append(Constants.DATA_CUT);
            sb.Append(groupScope); sb.Append(Constants.DATA_CUT);
            sb.Append(groupType); sb.Append(Constants.DATA_CUT);

            sb.Append(OldGroup.managedBy); sb.Append(Constants.DATA_CUT);       //Managed by- Old info

            sb.Append(members); sb.Append(Constants.DATA_CUT);


            // Append Old group infomation
            

            sb.Append(OldGroup.name); sb.Append(Constants.DATA_CUT);
            sb.Append(OldGroup.displayName); sb.Append(Constants.DATA_CUT); //displayName
            sb.Append(OldGroup.description); sb.Append(Constants.DATA_CUT);
            sb.Append(OldGroup.groupId); sb.Append(Constants.DATA_CUT);   //goupID
            sb.Append(OldGroup.LDAPPath); sb.Append(Constants.DATA_CUT);
            sb.Append((int)OldGroup.groupScope); sb.Append(Constants.DATA_CUT);
            sb.Append(((bool)OldGroup.isSecurityGroup)?1:0); sb.Append(Constants.DATA_CUT);
            sb.Append(OldGroup.managedBy); sb.Append(Constants.DATA_CUT);       //Managed by
            sb.Append(OldGroup.members); sb.Append(Constants.DATA_CUT);
            return sb.ToString();
        }

        public HT_Groups GetHT_GroupByName(string groupName)
        {
            HT_GroupsDAO GroupDAO = new HT_GroupsDAO();
            return GroupDAO.GetHT_GroupsByName(groupName);
        }

        public HT_Groups GetNewHTGroupFromEditUserData(string data)
        {
            HT_Groups group = new HT_Groups();
            string[] Arr = data.Split(Constants.DATA_CUT);
            group.name = Arr[0];
            group.displayName = Arr[1];
            group.description = Arr[2];
            if (!string.IsNullOrEmpty(Arr[3])) group.groupId = Guid.Parse(Arr[3]);
            group.LDAPPath = Arr[4];
            group.groupScope = Convert.ToInt16(Arr[5]);
            group.isSecurityGroup = (Convert.ToInt16(Arr[6]) > 0);
            group.managedBy = Arr[7];
            group.members = Arr[8];
            return group;
        }

        public HT_Groups GetOldHTGroupFromEditUserData(string data)
        {
            HT_Groups group = new HT_Groups();
            string[] Arr = data.Split(Constants.DATA_CUT);
            group.name = Arr[9];
            group.displayName = Arr[10];
            group.description = Arr[11];
            if (!string.IsNullOrEmpty(Arr[12])) group.groupId = Guid.Parse(Arr[12]);
            group.LDAPPath = Arr[13];
            group.groupScope = Convert.ToInt16(Arr[14]);
            group.isSecurityGroup = (Convert.ToInt16(Arr[15]) > 0);
            group.managedBy = Arr[16];
            group.members = Arr[17];
            return group;
        }



        public string BuildAddGroupString(string groupName, string description, string LDAPPath, int groupScope, int groupType, string members)
        {
            StringBuilder sb = new StringBuilder();
            // Append new group infomation
            sb.Append(groupName); sb.Append(Constants.DATA_CUT);

            sb.Append(""); sb.Append(Constants.DATA_CUT); //displayName - Old info

            sb.Append(description); sb.Append(Constants.DATA_CUT);

            sb.Append(""); sb.Append(Constants.DATA_CUT);   //goupID - old info

            sb.Append(LDAPPath); sb.Append(Constants.DATA_CUT);
            sb.Append(groupScope); sb.Append(Constants.DATA_CUT);
            sb.Append(groupType); sb.Append(Constants.DATA_CUT);

            sb.Append(""); sb.Append(Constants.DATA_CUT);       //Managed by- Old info

            sb.Append(members); sb.Append(Constants.DATA_CUT);
            return sb.ToString();
        }

        public HT_Groups GetHTGroupFromAddGroupData(string data)
        {
            HT_Groups group = new HT_Groups();
            string[] Arr = data.Split(Constants.DATA_CUT);
            group.name = Arr[0];
            group.displayName = Arr[1];
            group.description = Arr[2];
            if(!string.IsNullOrEmpty(Arr[3])) group.groupId = Guid.Parse(Arr[3]);
            group.LDAPPath = Arr[4];
            group.groupScope = Convert.ToInt16(Arr[5]);
            group.isSecurityGroup = (Convert.ToInt16(Arr[6]) > 0);
            group.managedBy = Arr[7];
            group.members = Arr[8];
            return group;
        }


        internal ReturnValue<bool> DelHT_Groups(string name)
        {
            HT_GroupsDAO _dao = new HT_GroupsDAO();
            return _dao.DelHT_Groups(name);
        }

        public string BuildEditGroupMemberString(string groupName, List<string> addList, List<string> removeList)
        {
            //Data: GroupName|addeduser1*addeduser2|removeuser1*removeuser2
            StringBuilder sb = new StringBuilder();
            sb.Append(groupName); sb.Append(Constants.DATA_CUT);
            sb.Append(Constants.MEMBERSPLIT);

            //AddList
            foreach (string add in addList)
            {
                sb.Append(add); sb.Append(Constants.DATA_CUT);
            }
            sb.Append(Constants.MEMBERSPLIT);
            //RemoveList
            foreach (string remove in removeList)
            {
                sb.Append(remove); sb.Append(Constants.DATA_CUT);
            }
            return sb.ToString();
        }

        public HT_Groups GetHT_GroupFromEditGroupMemeberData(string data)
        {
            string[] Arr = data.Split(Constants.MEMBERSPLIT);
            string[] GroupArr = Arr[0].Split(Constants.DATA_CUT);
            HT_GroupsBO BO = new HT_GroupsBO();
            return BO.GetHT_GroupByName(GroupArr[0]);
        }
        public List<string> GetAddListFromEditGroupMemeberData(string data)
        {
            string[] Arr = data.Split(Constants.MEMBERSPLIT);
            string[] UserArr = Arr[1].Split(Constants.DATA_CUT);
            return new List<string>(UserArr);
        }
        public List<string> GetRemoveListFromEditGroupMemeberData(string data)
        {
            string[] Arr = data.Split(Constants.MEMBERSPLIT);
            string[] UserArr = Arr[2].Split(Constants.DATA_CUT);
            return new List<string>(UserArr);
        }
    }
}
